package com.javaclimb.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.javaclimb.music.domain.Singer;
import com.javaclimb.music.service.SingerService;
import com.javaclimb.music.utils.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/singer")
public class SingerController {

    @Autowired
    private SingerService singerService;
    /**
     * 显示所有歌手信息
     * @return
     */
    @RequestMapping(value = "/selectAll",method = RequestMethod.GET)
    public List<Singer> selectAll(){
        return singerService.selectAll();
    }
    /**
     * 添加歌手信息
     * @param singer
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Object addSinger(Singer singer){
        //fastjson就量个json的转换工具
        JSONObject jsonObject = new JSONObject();
        //调用Service中的方法
        int flag = singerService.addSinger(singer);
        if (flag==1){
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"添加成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE,0);
        jsonObject.put(Consts.MSG,"添加失败");
        return jsonObject;
//        return singerService.addSinger(singer);
    }

    /**
     * 根据id删除歌手信息
     * @param
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Object deleteSinger(Singer singer){
        return singerService.deleteSinger(singer.getId());

    }
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Object updateSinger(Singer singer){
        JSONObject jsonObject = new JSONObject();
        int flag = singerService.updateSinger(singer);
        if(flag == 1){
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"修改成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE,0);
        jsonObject.put(Consts.MSG,"修改失败");
        return jsonObject;
    }

    /**
     * 模糊查询
     * @param singer
     * @return
     */
    @RequestMapping(value = "/singerOfName",method = RequestMethod.GET)
    public Object singerOfName(Singer singer) {
        return singerService.singerOfName(singer.getName());
    }

    /**
     * 更新图片
     * @param avatorFile
     * @param id
     * @return
     */
    @RequestMapping(value = "/updateSingerPic",method = RequestMethod.POST)
    public Object updateSingerPic(@RequestParam("file") MultipartFile avatorFile, @RequestParam("id")int id) {
        JSONObject jsonObject = new JSONObject();
        if(avatorFile.isEmpty()) {
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"文件上传失败");
            return jsonObject;
        }
        //文件名 = 当前的系统时间到毫秒+原文件名
        String fileName = System.currentTimeMillis() + avatorFile.getOriginalFilename();
        // 构造临时路径来存储上传的文件, 这个路径相对当前应用的目录
        String filePath = System.getProperty("user.dir")+
                System.getProperty("file.separator")+"img"+
                System.getProperty("file.separator")+"singerPic";
        // 如果目录不存在则创建
        File file = new File(filePath);
        if (!file.exists()) {
            //创建此抽象路径名指定的目录，包括所有必需但不存在的父目录。
            file.mkdirs();
        }
        //实际文件地址
        File destFile = new File(filePath+ System.getProperty("file.separator")+fileName);
        //更新路径
        String storeAvatorPath = "/img/singerPic/"+fileName;
        try {
            //上传文件
            avatorFile.transferTo(destFile);
            //将路径更新
            Singer singer = new Singer();
            singer.setId(id);
            singer.setPic(storeAvatorPath);
            int flag = singerService.updateSinger(singer);
            if(flag==1) {
                jsonObject.put(Consts.CODE,1);
                jsonObject.put(Consts.MSG,"文件上传成功");
                jsonObject.put("pic",storeAvatorPath);
                return jsonObject;
            }
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"文件上传失败");
            return jsonObject;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

}
